use salvo::{macros::Extractible, oapi::ToSchema};
use serde::{Deserialize, Serialize};
use validator::Validate;
use crate::entities::papers::Paper;

#[derive(Deserialize, Debug, ToSchema, Default, Validate)]
pub struct PaperAddRequest {
    #[validate(length(min = 1, message = "title is required"))]
    pub title: String,
    pub author: String,
    pub abstract_: String,
}

#[derive(Debug, Deserialize,Extractible,ToSchema, Default)]
#[salvo(extract(default_source(from = "body", parse = "json")))]
pub struct PaperUpdateRequest {
    #[salvo(extract(source(from = "param")))]
    pub id: String,
    pub title: String,
    pub author: String,
    pub abstract_: String,
}

#[derive(Debug, Serialize, ToSchema, Default, Clone)]
pub struct PaperResponse {
    pub id: String,
    pub title: String,
    pub author: String,
    pub abstract_: String,
}

impl From<Paper> for PaperResponse {
    fn from(value: Paper) -> Self {
        Self {
            id: value.id,
            title: value.title,
            author: value.author,
            abstract_: value.abstract_,
        }
    }
}
impl From<PaperResponse> for Paper {
    fn from(req: PaperResponse) -> Self {
        Self {
            id: req.id,
            title: req.title,
            author: req.author,
            abstract_: req.abstract_,
        }
    }
}